<?php

namespace plugin\coupon\app\admin\controller;

use app\admin\controller\Curd;
use plugin\coupon\app\admin\service\CouponService;
use plugin\coupon\app\model\Coupon;
use plugin\coupon\app\model\CouponReceiveLog;
use support\Request;

class IndexController extends Curd
{
    public function index()
    {
        if (request()->isAjax()) {
            $couponService = new CouponService();
            $res = $couponService->getList(request()->all());
            return sparkSuccess($res);
        }
        return view('index/index');
    }

    /**
     * 添加优惠券
     */
    public function add()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $couponService = new CouponService();
            $res = $couponService->addCoupon($param);
            return sparkSuccess($res);
        }
        if (request()->isAjax()) {
            return sparkSuccess(dataReturn(0,'ok',toArray((new CouponService())->getCouponGoodsList(request()->input('id')))));
        }
        return view('index/add');
    }
    /**
     * 作废优惠券
     */
    public function close()
    {
        $id = request()->input('id');
        $couponModel = new Coupon();
         $couponModel->updateById([
            'status' => 2,
            'update_time' => now()
        ], $id);
        return sparkSuccess(dataReturn(0,'操作成功'));
    }

    /**
     * 领取记录
     */
    public function log()
    {
        if (request()->isAjax()) {
            $id =  request()->input('id');
            $limit =  request()->input('limit');
            $couponReceiveModel = new CouponReceiveLog();
            $list=$couponReceiveModel->getPaginateList(['coupon_id'=>$id],['*'],[],[],$limit);
            return sparkSuccess(dataReturn(0, 'success',$list ));
        }
    }

}